import java.sql.*;

public class Main {
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://10.27.200.18:3306/demo";
    static final String USER = "root";
    static final String PASS = "java2024";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();

            String sql = "SELECT s.name AS student_name, s.age, s.gender, s.grade " +
                         "FROM students s " +
                         "JOIN course_selection cs ON s.student_id = cs.student_id " +
                         "JOIN courses c ON cs.course_id = c.course_id " +
                         "WHERE c.course_name = 'Computer Science'";

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                String studentName = rs.getString("student_name");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                int grade = rs.getInt("grade");

                System.out.println("Student Name: " + studentName);
                System.out.println("Age: " + age);
                System.out.println("Gender: " + gender);
                System.out.println("Grade: " + grade);
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }
}
